package com.aerozhonghuan.logisticsmall.utils;

import android.content.Context;
import android.util.Log;

import com.aerozhonghuan.logisticsmall.framework.DB.DBAdapter;
import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;

/**
 * Created by ChenLin on 2019/5/21 14:08.Email&WeChat:7877409@163.com
 */
public class CommunityUtils {

    private static int getAllGouwucheSize(DBAdapter mDbAdapter) {
        int totalSize = 0;
        ArrayList<GoodsObject> getData = mDbAdapter.getAllData();
        for (int i = 0; i < getData.size(); i++) {
            GoodsObject goodsObject = getData.get(i);
            if (goodsObject.getIsCheck() == 0) {
                totalSize += goodsObject.getIntoGouwucheSize();
            }
        }
        return totalSize;
    }

    /**
     * sourceId 值
     * 1 老版本商品 2 首页 3 搜索页 4 超市 5 商品详情 6 三列商品页面 7 单列商品页面 8 我常买 9 扫一扫 10 Web页面
     */
    public static int addProductToGouwuche(Context context, GoodsObject product, DBAdapter mDbAdapter, int sourceId) {
        int mAppIndex = 101;
        switch (sourceId) {
            case 1:
                mAppIndex = 101;
                break;
            case 2:
                mAppIndex = 101;
                break;
            case 3:
                mAppIndex = 108;
                break;
            case 4:
                mAppIndex = 110;
                break;
            case 5:
                mAppIndex = 103;
                break;
            case 6:
                mAppIndex = 102;
                break;
            case 7:
                mAppIndex = 106;
                break;
            case 8:
                mAppIndex = 419;
                break;
            case 9:
                mAppIndex = 409;
                break;
            case 10:
                mAppIndex = 102;
                break;
        }
        int productSize = 0;
        if (product != null) {
            GoodsObject object = mDbAdapter.open().getRecordFromProductId(
                    product.getPvsid());
            productSize = object.getIntoGouwucheSize();
            product.setSourceId(sourceId);
            if (productSize >= product.getQuantityAvailable()) {
                if (product.getProductType() == 3) {
                    ToastUtils.showShort("库存不足了，先借这么多吧");
                } else {
                    ToastUtils.showShort("库存不足了，先买这么多吧");
                }
                int totalSize = getAllGouwucheSize(mDbAdapter);
                StatisticsConstant.getInstance().ClickActionParams(5, mAppIndex, String.valueOf(product.getPvsid()));
                return totalSize;
            }
            if (product.getMax() != 0) {
                if (productSize >= product.getMax()) {
                    ToastUtils.showShort("亲,此商品限购\" + productSize + \"件!");
                    int totalSize = getAllGouwucheSize(mDbAdapter);
                    return totalSize;
                }
            }
            productSize++;
            product.setIntoGouwucheSize(productSize);
            int update = mDbAdapter.open().updateGouwuche
                    (product.getPvsid(), CommunityUtils.USER_ID, CommunityTool.saveObject(product));
            if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                Log.d(CommunityUtils.TAG,
                        "gouwucheListener:" + product.getBaseName() + " " + product.getPostfixName() + " "
                                + product.getExtName() + "update:"
                                + update);
            if (update <= 0) {
                long value = mDbAdapter.open().insertGouwuche
                        (product.getPvsid(), CommunityUtils.USER_ID, CommunityTool.saveObject(product), product.getProductType());
                if (com.blankj.utilcode.util.AppUtils.isAppDebug())
                    Log.d(CommunityUtils.TAG,
                            "insertGouwuche:" + value);
            }
            //T.show(context, "已加入购物车");
            StatisticsConstant.getInstance().ClickActionParams(4, mAppIndex, String.valueOf(product.getPvsid()));
        } else {
            ToastUtils.showShort("添加购物车失败");
        }
        int totalSize = getAllGouwucheSize(mDbAdapter);
        return totalSize;//返回购物车商品总数
    }

    public static int USER_ID = 0;

    public static final String TAG = CommunityUtils.class.getSimpleName();

}
